#!/usr/bin/python
#coding=utf-8
import sys
from sys import argv

reload(sys)
sys.setdefaultencoding('utf-8')
script,imageName,userfor=argv
userfor=userfor.strip()

import pytesseract
from PIL import Image,ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True


img = Image.open(imageName)
code = pytesseract.image_to_string(img,lang="num5")


###########以下为正则方式提取数据
import re
#pattern = re.compile(r'[\u4e00-\u9fa5]\d{1,3}[\u4e00-\u9fa5]')
#pattern = re.compile(r'[\u8005]\d+[\u533a]')
if userfor=='server':
    str='[者|再](\d{1,3})区'.decode('utf-8')
elif userfor=='group':
    str='(\d{1,3}-\d{3})'.decode('utf-8')
elif userfor=='money':
    str = '[者|再](\d{1,3})区'.decode('utf-8')
    print code
    pass

pattern = re.compile(str)

result= pattern.findall(code)
if len(result)<1:
    import os
    import time
    os.popen('copy %s %s'%(imageName,'E:\mobileCharge\image\server\unknow\%s_%f.%s'%(time.strftime('%Y%m%d'),time.time(),imageName.split('.')[-1])) )


print ','.join(result)
